package com.tcc.task.Community;

import org.apache.xpath.operations.String;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.tcc.login.logic.Login;

public class CategoryManager {
	WebDriver driver = new FirefoxDriver();
	String s1 = "Work";
	String s2 = "Study";

	@BeforeTest
	public void login() throws InterruptedException {
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		Login myLogin = new Login();
		myLogin.testLogin(driver);
	}

	@BeforeMethod
	public void beforeTestMethod() {
		System.out.println("Before Test Method");
	}

	@Test(description = "Click Personal Task Icon and go to My Tasks Page", priority = 1)
	public void taskLinkIcon() throws InterruptedException {
		WebDriverWait wait = new WebDriverWait(driver, 10);
		wait.until(new ExpectedCondition<WebElement>() {
			@Override
			public WebElement apply(WebDriver d) {
				return d.findElement(By.id("qbtasks"));
			}
		}).click();

		AssertJUnit.assertEquals(driver.getTitle().toString(),
				" My Tasks @ MyCruiser > Personal Tools ");
	}

	@Test(description = "Add category without name", priority = 2)
	public void add_nullName() {
		driver.findElement(By.xpath("//*[@id='ccTgArea']/form/table/tbody/tr/td[2]/a")).click();
		driver.findElement(By.xpath("//*[@id='btns']/button[1]")).click();
		driver.switchTo().alert().sendKeys("");
		driver.switchTo().alert().accept();
		Assert.assertEquals(driver.switchTo().alert().getText(),
				"You must enter a name.");
		driver.switchTo().alert().accept();
	}

	@Test(description = "Add new category",priority = 3)
	public void add_Category() {
		driver.findElement(By.xpath("//*[@id='btns']/button[1]")).click();
		driver.switchTo().alert().sendKeys(s1);
		driver.switchTo().alert().accept();
		AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='categories']/option[1]")).getText(),s1);
	}

	@Test( description = "Edit the category with an exist name",priority = 4)
	public void edit_sameName() {
		driver.findElement(By.xpath("//select[@id='categories']/option[1]")).click();
		driver.findElement(By.xpath("//button[@type='BUTTON'][2]")).click();
		driver.switchTo().alert().sendKeys(s1);
		driver.switchTo().alert().accept();
		Assert.assertEquals(driver.switchTo().alert().getText(),
				"Entry already available.  Please choose another name.");
		driver.switchTo().alert().accept();
	}

	@Test(description = "Rename the category",priority = 5)
	public void edit_Rename() {
		driver.findElement(By.xpath("//button[@type='BUTTON'][2]")).click();
		driver.switchTo().alert().sendKeys(s2);
		driver.switchTo().alert().accept();
		AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='categories']/option[1]")).getText(),s2);
	}

	@Test(description = "Delete the category",priority = 6)
	public void deleteCategory() {
		driver.findElement(By.xpath("//select[@id='categories']/option[1]"))
				.click();
		driver.findElement(By.xpath("//button[@type='BUTTON'][3]")).click();
		Assert.assertEquals(driver.findElement(By.xpath(".//*[@id='categories']")).getText().toString(),"");
		//Assertnull
	}

	@Test(description = "Save the changes and check the successful message",priority = 7)
	public void save_message() {
		driver.findElement(By.xpath("//*[@id='btns']/button[1]")).click();
		driver.switchTo().alert().sendKeys(s1);
		driver.switchTo().alert().accept();
		driver.findElement(By.xpath("//input[@value='Save']")).click();
		AssertJUnit.assertEquals(
				driver.findElement(By.xpath("//div[@id='ccTgArea']/div[2]"))
						.getText(), "Categories have been saved successfully.");
	}

	@Test(description = "check whether saved or not",priority = 8)
	public void save_check() {
		Assert.assertEquals(
				driver.findElement(
						By.xpath("//select[@id='categories']/option[1]"))
						.getText(), s1);
	}

	@Test(description = "Reset after add a new category",priority = 9)
	public void Reset() {
		driver.findElement(By.xpath("//*[@id='btns']/button[1]")).click();
		driver.switchTo().alert().sendKeys(s2);
		driver.switchTo().alert().accept();
		driver.findElement(By.xpath("//input[@value='Reset']")).click();
		AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='categories']/option[1]")).getText(),s1);
	}

	@Test(description = "Cancel after add a new Category",priority = 10)
	public void Cancle() {
		driver.findElement(By.xpath("//*[@id='btns']/button[1]")).click();
		driver.switchTo().alert().sendKeys(s2);
		driver.switchTo().alert().accept();
		driver.findElement(By.xpath("//input[@value='Cancel']")).click();
		Assert.assertEquals(driver.getTitle().toString(),
				" My Tasks @ MyCruiser > Personal Tools ");
		driver.get("https://prod.campuscruiser.com/q?cx=u&pg=papp&tg=Tasks-prefCategory");
		AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='categories']/option[1]")).getText(),s1);
		System.out.println("Now there is a category which named work");
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("End the method test.");

	}

	@AfterTest
	public void endTest() {
		System.out.println("End the test.");

	}

}
